# coding: utf-8
from sqlalchemy import Column, Date, DateTime, Float, Index, Integer, Numeric, String, Time, text
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.declarative import as_declarative
from sqlalchemy.ext.declarative import declared_attr

# Base = declarative_base()
# metadata = Base.metadata


@as_declarative()
class Base(object):
    @declared_attr
    def __tablename__(cls):
        return 'EM_'+cls.__name__.upper()

    def columns(self):
        return [c.name for c in self.__table__.columns]

    def to_dict(self):
        return dict([(c, getattr(self, c)) for c in self.columns()])

    def validate(self):
        return False


class BaseTradeHis(Base):

    tradeHisId = Column(Integer, primary_key=True)
    tradeDay = Column(Date)
    tradeTime = Column(Time)
    stkCode = Column(String(32))
    stkName = Column(String(32))
    tradeType = Column(String(32))
    tradePrice = Column(Float)
    tradeCount = Column(Numeric(10, 0))
    totalMoney = Column(Float)
    tradeSource = Column(String(32))

    def __init__(self, tradeDay, tradeTime, stkCode, stkName, tradeType, tradePrice, tradeCount, tradeTotalMoney, operationDetail, tradeSource):
        self.tradeDay = tradeDay
        self.tradeTime = tradeTime
        self.stkCode = stkCode
        self.stkName = stkName
        self.tradeType = tradeType
        self.tradePrice = tradePrice
        self.tradeCount = abs(tradeCount)
        self.totalMoney = tradeTotalMoney
        self.operationDetail = operationDetail
        self.tradeSource = tradeSource

    def validate(self):
        if (self.operationDetail == '证券买入' or self.operationDetail == '证券卖出') and self.stkName != '报价标准券':
            return True
        return False

class BasicData(Base):

    __table_args__ = (
        Index('AK_UQ_code', 'stockcode', 'market', unique=True),
    )

    ID = Column(Integer, primary_key=True)
    stockcode = Column(String(10), nullable=False)
    market = Column(String(10), nullable=False)
    C_name = Column(String(100))
    ipodate = Column(DateTime)
    industry_zj = Column(Integer)
    industry_sw = Column(Integer)
    createtime = Column(DateTime)
    info = Column(String(500))
    is_del = Column(Integer, nullable=False, server_default=text("'0'"))


class DividendData(Base):

    ID = Column(Integer, primary_key=True)
    stockid = Column(Integer, nullable=False)
    executetime = Column(DateTime)
    dividend_share = Column(Numeric(10, 0))
    dividend_cash = Column(Numeric(10, 0))
    rationed_share = Column(Numeric(10, 0))
    floatshare_before = Column(Integer)
    floatshar_after = Column(Integer)
    createtime = Column(DateTime)
    is_valid = Column(Integer, nullable=False, server_default=text("'1'"))


class HisSeries(Base):

    ID = Column(Integer, primary_key=True)
    stockid = Column(Integer, nullable=False)
    datatime = Column(DateTime, nullable=False)
    period = Column(Integer)
    high = Column(Numeric(6, 2))
    low = Column(Numeric(6, 2))
    open = Column(Numeric(6, 2))
    close = Column(Numeric(6, 2))
    volume = Column(Integer)
    amount = Column(Integer)
    MACD = Column(Numeric(10, 0))
    KDJ = Column(Numeric(10, 0))
    MA5 = Column(Numeric(10, 0))
    createtime = Column(DateTime)
    is_valid = Column(Integer, nullable=False, server_default=text("'1'"))


class PositionData(Base):

    ID = Column(Integer, primary_key=True)
    stockid = Column(Integer, nullable=False)
    datatime = Column(DateTime, nullable=False)
    S1_v = Column(Integer)
    S2_v = Column(Integer)
    S3_v = Column(Integer)
    S4_v = Column(Integer)
    S5_v = Column(Integer)
    B1_v = Column(Integer)
    B2_v = Column(Integer)
    B3_v = Column(Integer)
    B4_v = Column(Integer)
    B5_v = Column(Integer)
    B1_p = Column(Numeric(6, 2))
    B2_p = Column(Numeric(6, 2))
    B3_p = Column(Numeric(6, 2))
    B4_p = Column(Numeric(6, 2))
    B5_p = Column(Numeric(6, 2))
    S1_p = Column(Numeric(6, 2))
    S2_p = Column(Numeric(6, 2))
    S3_p = Column(Numeric(6, 2))
    S4_p = Column(Numeric(6, 2))
    S5_p = Column(Numeric(6, 2))
    changerate_of_price = Column(Numeric(10, 4))
    changerange = Column(Numeric(10, 4))
    changerate_of_vol = Column(Numeric(10, 4))
    in_vol = Column(Integer)
    out_vol = Column(Integer)
    increasing_rate = Column(Numeric(10, 4))
    exchange_rate = Column(Numeric(10, 4))
    num_increasing = Column(Integer)
    num_decreasing = Column(Integer)
    num_top = Column(Integer)
    num_bottom = Column(Integer)
    trade = Column(Numeric(6, 2))
    high = Column(Numeric(6, 2))
    low = Column(Numeric(6, 2))
    open = Column(Numeric(6, 2))
    volume = Column(Integer)
    amount = Column(Integer)
    is_valid = Column(Integer, nullable=False, server_default=text("'1'"))


class PositionInfo(Base):

    ID = Column(Integer, primary_key=True)
    userid = Column(Integer, nullable=False)
    stockid = Column(Integer, nullable=False)
    position = Column(Integer)
    aver_cost = Column(Numeric(10, 0))
    cost = Column(Numeric(10, 0))
    is_position = Column(Integer, server_default=text("'0'"))
    profit_ratio = Column(Numeric(10, 0))
    createtime = Column(DateTime)
    modifytime = Column(DateTime)


class RecheckData(Base):

    ID = Column(Integer, primary_key=True)
    stockid = Column(Integer, nullable=False)
    btime = Column(DateTime, nullable=False)
    etime = Column(DateTime, nullable=False)
    benifit_count = Column(Integer)
    trade_num = Column(Integer)
    winning_rate = Column(Numeric(10, 0))
    fee = Column(Numeric(10, 0))
    retained_profits = Column(Numeric(10, 0))
    _yield = Column('yield', Numeric(10, 0))
    HC_MAX = Column(Numeric(10, 0))
    HC_MAX_R = Column(Numeric(10, 0))
    createtime = Column(DateTime)
    modifytime = Column(DateTime)


class StatementData(Base):

    ID = Column(Integer, primary_key=True)
    stockid = Column(Integer, nullable=False)
    datatime = Column(DateTime)
    ZGB = Column(Integer)
    circulation_A = Column(Integer)
    circulation_B = Column(Integer)
    circulation = Column(Integer)
    eps = Column(Numeric(10, 4))
    pe = Column(Numeric(10, 4))
    pb = Column(Numeric(10, 4))
    is_valid = Column(Integer, nullable=False, server_default=text("'1'"))
    createtime = Column(DateTime)
    modifytime = Column(DateTime)


class SysIndustryInfo(Base):

    ID = Column(Integer, primary_key=True)
    name = Column(String(100))
    adscription = Column(String(10))
    createtime = Column(DateTime)
    modifytime = Column(DateTime)


class SysModuleInfo(Base):

    ID = Column(Integer, primary_key=True)
    name = Column(String(100))
    createtime = Column(DateTime)
    modifytime = Column(DateTime)


class Tick(Base):

    ID = Column(Integer, primary_key=True)
    stockid = Column(Integer, nullable=False)
    datatime = Column(DateTime)
    price = Column(Numeric(10, 0))
    volume = Column(Integer)
    amount = Column(Numeric(10, 0))
    dataType = Column(Integer)
    is_valid = Column(Integer, nullable=False, server_default=text("'1'"))


class TradeRecord(Base):

    ID = Column(Integer, primary_key=True)
    userid = Column(Integer, nullable=False)
    stockid = Column(Integer, nullable=False)
    is_sale = Column(Integer, nullable=False)
    tradetime = Column(DateTime, nullable=False)
    trade_num = Column(Integer, nullable=False)
    unit_price = Column(Numeric(10, 0), nullable=False)
    fee = Column(Numeric(10, 0))


class UserInfo(Base):

    ID = Column(Integer, primary_key=True)
    user_name = Column(String(100), nullable=False)
    psd = Column(String(150))
    trade_account = Column(String(100))
